package com.mmy.controller.line;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.line.TLineOrder;
import com.mmy.domain.line.TLinePlan;
import com.mmy.domain.line.TLineTrip;
import com.mmy.domain.line.VLineBase;
import com.mmy.domain.line.VLineOrder;
import com.mmy.domain.line.VLinePlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.line.LineBaseServiceImpl;
import com.mmy.service.impl.line.LineFrontServiceImpl;
import com.mmy.service.impl.line.LineOrderServiceImpl;
import com.mmy.service.impl.line.LinePlanServiceImpl;
import com.mmy.service.impl.line.LineTripServiceImpl;

@Controller
public class LineFrontController {

	private static Logger logger = LoggerFactory.getLogger(LineFrontController.class);

	@Autowired
	LinePlanServiceImpl linePlanServiceImpl;
	@Autowired
	LineOrderServiceImpl lineOrderServiceImpl;
	@Autowired
	LineFrontServiceImpl lineFrontServiceImpl;
	@Autowired
	LineBaseServiceImpl lineBaseServiceImpl;
	@Autowired
	LineTripServiceImpl lineTripServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;

	/**
	 * 函数功能说明：跳转--近郊游列表页
	 *
	 * @param 参数说明 vLineBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 修改liys
	 * @date 创建日期 2020.08.18 修改时间 2020.01.04
	 */
	@RequestMapping("/surroudingtourism")
	public String surroudingTourism(HttpServletRequest request, Model model, String pageNumber, String pageSize,
			String type) {

		Integer iType = Integer.parseInt(type);
		model.addAttribute("type", iType);
		List<TConstZone> lstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstZoneProvince", lstZoneProvince);
		List<Integer> lstLineID = linePlanServiceImpl.findSurroudingNum();
		List<VLineBase> lstLineBaseSurrouding = lineBaseServiceImpl.findSurroudingByPlan(lstLineID, pageNumber,
				pageSize, iType);
		if (iType != 3) {
			for (VLineBase vlineBase : lstLineBaseSurrouding) {
				String location = constZoneServiceImpl.findLocation(vlineBase.getZoneID());
				vlineBase.setZoneName(location);
			}
		} else if (iType == 3) {
			for (VLineBase vlineBase : lstLineBaseSurrouding) {
				TConstZone constZone = constZoneServiceImpl.findCountry(vlineBase.getZoneID());
				vlineBase.setZoneName(constZone.getZoneName());
			}
		}
		model.addAttribute("lstLineBaseSurrouding", lstLineBaseSurrouding);
		List<VLinePlan> lstSurroudingPlan = linePlanServiceImpl.findNowSurrouding(iType);
		model.addAttribute("lstSurroudingPlan", lstSurroudingPlan);
		Map<String, Integer> mapPage = lineBaseServiceImpl.mapLineBaseFront(lstLineID, pageNumber, pageSize, iType);
		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		return "touristroutes/tour";
	}

	/**
	 * 函数功能说明：跳转--条件搜索后的线路列表
	 *
	 * @param 参数说明 vLineBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 liys
	 * @date 创建日期 2021.1.5
	 */

	@RequestMapping("/querysurroudingtourism")
	public String querySurroudingTourism(HttpServletRequest request, Model model, String type) {

		Integer iType = Integer.parseInt(type);
		model.addAttribute("type", iType);
		String strProvince = request.getParameter("Province");
		String strCity = request.getParameter("City");
		String strZoneID = request.getParameter("ZoneID");
		String strDay = request.getParameter("strDay");
		List<TConstZone> lstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstZoneProvince", lstZoneProvince);
		List<Integer> lstLineID = linePlanServiceImpl.findSurroudingNum();
		List<VLineBase> lstLineBaseSurrouding = lineBaseServiceImpl.findSurroudingByPlanQuery(lstLineID, strZoneID,
				strCity, strProvince, strDay, iType);
		if (iType != 3) {
			for (VLineBase vlineBase : lstLineBaseSurrouding) {
				String location = constZoneServiceImpl.findLocation(vlineBase.getZoneID());
				vlineBase.setZoneName(location);
			}
		} else if (iType == 3) {
			for (VLineBase vlineBase : lstLineBaseSurrouding) {
				TConstZone constZone = constZoneServiceImpl.findCountry(vlineBase.getZoneID());
				vlineBase.setZoneName(constZone.getZoneName());
			}
		}
		model.addAttribute("lstLineBaseSurrouding", lstLineBaseSurrouding);
		List<VLinePlan> lstSurroudingPlan = linePlanServiceImpl.findNowSurrouding(iType);
		model.addAttribute("lstSurroudingPlan", lstSurroudingPlan);
		return "touristroutes/tour";
	}

	/**
	 * 函数功能说明： 跳转--线路信息详情
	 *
	 * @param 参数说明 tLineBase
	 * @return 返回值说明 jsp
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期 2021.1.4
	 */

	@RequestMapping("/linebaseinfofront")
	public String toLineBaseInfoFront(HttpServletRequest request, Model model) {

		int iLineID = Integer.parseInt(request.getParameter("LineID"));
		VLineBase vLineBase = lineBaseServiceImpl.findByLineID(iLineID);
		String location = constZoneServiceImpl.findLocation(vLineBase.getZoneID());
		List<TLineTrip> lstLineTrip = lineTripServiceImpl.findByLineID(iLineID);
		List<VLinePlan> lstSurroudingPlan = linePlanServiceImpl.findByLineIdNow(iLineID);
		// 拆分、拼接 集合地+时间
		List<Map<String, Object>> lstPlace = new ArrayList<>();
		String strPlace = vLineBase.getPlaceID();
		String strTime = vLineBase.getTime();
		String[] arrPlace = strPlace.split(",");
		String[] arrTime = strTime.split(",");
		for (int i = 0; i < arrPlace.length; i++) {
			Map<String, Object> map = new HashMap<>();
			map.put("place", arrPlace[i]);
			map.put("time", arrTime[i]);
			lstPlace.add(map);
		}
		model.addAttribute("place", lstPlace);
		model.addAttribute("lstSurroudingPlan", lstSurroudingPlan);
		model.addAttribute("vLineBase", vLineBase);
		model.addAttribute("location", location);
		model.addAttribute("lstLineTrip", lstLineTrip);
		return "touristroutes/LineBaseInfoFront";
	}

	/**
	 * 函数功能说明： 异步--根据时间查询线路当前计划
	 *
	 * @param 参数说明 Date,iLineID
	 * @return 返回值说明 map
	 * @author 创建人 liys
	 * @date 创建日期 2021.1.11
	 */
	@RequestMapping("/queryplan")
	@ResponseBody
	public Map<String, Object> QueryPlan(@RequestParam("date") Date date, @RequestParam("lineID") String lineID) {

		Integer iLineID = Integer.parseInt(lineID);
		List<Map<String, Object>> result = linePlanServiceImpl.findPlanValidMap(iLineID, date);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("standard", result);
		return map;
	}

	/**
	 * 函数功能说明： 异步--线路预留页查询价格
	 *
	 * @param 参数说明
	 * @return 返回值说明 String
	 * @author 创建人 liys
	 * @date 创建日期 2021.1.11
	 */
	@RequestMapping("/queryprice")
	@ResponseBody
	public String queryPrice(@RequestParam("plan") String strJson) {

		List<TLinePlan> lstLinePlan = JSON.parseArray(strJson, TLinePlan.class);
		BigDecimal total = lineFrontServiceImpl.queryPrice(lstLinePlan);
		return total.toString();
	}

	/**
	 * 函数功能说明： 跳转--预订填信息页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期 2021.1.7
	 */
	@RequestMapping("/toreserveline")
	public String toReserveLine(HttpServletRequest request, Model model) {

		int iLineID = Integer.parseInt(request.getParameter("iLineID"));
		List<VLinePlan> lstLinePlan = linePlanServiceImpl.findPlanValid(iLineID);
		String list = lineFrontServiceImpl.getPriceCalendar(lstLinePlan);
		model.addAttribute("lstLinePlan", lstLinePlan);
		model.addAttribute("iLineID", iLineID);
		model.addAttribute("json", list);
		return "touristroutes/ReserveLine";
	}

	/**
	 * 函数功能说明：提交--新建线路预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 liys
	 * @throws Exception
	 * @date 修改日期 2021.01.27
	 */
	@RequestMapping("/savereserveline")
	public String saveReserveLine(@RequestParam("plan") String strLinePlan, @RequestParam("order") String strLineOrder,
			@RequestParam("lineDate") String strLineDate,@RequestParam("lineID") String strLineID, HttpSession session) {
		
		int lineID = Integer.parseInt(strLineID);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		TLineOrder tLineOrder = new TLineOrder();
		List<TLinePlan> lstLinePlan = JSON.parseArray(strLinePlan, TLinePlan.class);
		List<VLineOrder> lstLineOrder = JSON.parseArray(strLineOrder, VLineOrder.class);
		for (VLineOrder vLineOrder : lstLineOrder) {
			tLineOrder.setReserveName(vLineOrder.getReserveName());
			tLineOrder.setReserveTel(vLineOrder.getReserveTel());
			tLineOrder.setMemo(vLineOrder.getMemo());
		}
		tLineOrder.setLineDate(lineFrontServiceImpl.changeDate(strLineDate));
		tLineOrder.setOrderNumber(lineFrontServiceImpl.planCount(lstLinePlan));
		tLineOrder.setOrderAmount(lineFrontServiceImpl.queryPrice(lstLinePlan));
		Date reserveDate = new Date();
		for (TLinePlan tLinePlan : lstLinePlan) {
			Integer planID = tLinePlan.getPlanID();
			Integer num = tLinePlan.getNumber();
			for (int i = 0; i < num; i++) {
				lineOrderServiceImpl.saveReserveLine(tLineOrder, tSysUser, planID, reserveDate,lineID);
			}
		}
		return "redirect:surroudingtourism";
	}

	/**
	 * 函数功能说明： 前台个人中心点击取消线路预留
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl 修改人shangzl
	 * @date 创建日期 2020.10.29 修改日期 2020.11.17
	 */

	@RequestMapping(value = "/enterlinecancelreserve")
	public String enterLineCancelReserve(HttpServletRequest request, HttpSession session) {
		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		lineOrderServiceImpl.updateState(iID, tSysUser);
		return "redirect:mylinereserve";
	}

	/**
	 * 函数功能说明： 前台个人中心点击申请线路退单
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl 修改人 shangzl
	 * @date 创建日期 2020.10.29 修改日期 2020.11.17
	 */
	@RequestMapping(value = "/tolineapplyrefund")
	public String toLineApplyRefund(HttpServletRequest request, HttpSession session) {
		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		lineOrderServiceImpl.updateRefundState(iID, tSysUser);
		return "redirect:mylineorder";
	}

	/**
	 * 函数功能说明： 前台个人中心点击撤销线路退单
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl 修改人 shangzl
	 * @date 创建日期 2020.10.29 修改日期 2020.11.17
	 */
	@RequestMapping(value = "/tolinecancelrefund")
	public String toLineCancelRefund(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		lineOrderServiceImpl.updateCancelState(iID, tSysUser);
		return "redirect:mylinerefund";
	}

	/**
	 * 函数功能说明： 前台个人中心显示订单详情
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl 修改人 shangzl
	 * @date 创建日期 2020.10.29 修改日期 2020.11.17
	 */
	@RequestMapping(value = "/mylineinfo")
	public String toMyLineInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		model.addAttribute("tLineOrder", tLineOrder);
		String strOrderID = tLineOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(9, 18));
		VLinePlan vLinePlan = linePlanServiceImpl.findByPlanID(iPlanID);
		model.addAttribute("vLinePlan", vLinePlan);
		return "permanagement/MyLineInfo";
	}
}
